package rocketman;
/*
Copyright (C) <year> <name of author>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import java.awt.Toolkit;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import java.util.*;

class PlayerLevel3 extends Player
{
String prefix = "./pics/";
Image leftimagestanding, rightimagestanding,leftshootimage, rightshootimage;
String direction = "nowhere", prevdirection = "right";
StateImageLibrary imagesleft;
protected int imagesindex = 0;
public PlayerLevel3(int startx, int starty)
{
	super(startx, starty);
	leftimagestanding = new ImageIcon(prefix+"playerman100x150-left-1.png").getImage();
	rightimagestanding = new ImageIcon(prefix+"playerman100x150-1.png").getImage();
	leftshootimage = new ImageIcon(prefix+"playerman150x150-shoot-left-1.png").getImage();
	rightshootimage = new ImageIcon(prefix+"playerman150x150-shoot-right-1.png").getImage();
	images = new StateImageLibrary();
        images.addImage("playerman100x150-walk-right-1.png");	
        images.addImage("playerman100x150-walk-right-2.png");	
        images.addImage("playerman100x150-walk-right-3.png");	
        images.addImage("playerman100x150-walk-right-4.png");	
        images.addImage("playerman100x150-walk-right-5.png");	
        images.addImage("playerman100x150-walk-right-6.png");	
        images.addImage("playerman100x150-walk-right-7.png");	
        images.addImage("playerman100x150-walk-right-8.png");	
        images.addImage("playerman100x150-walk-right-9.png");	
        images.addImage("playerman100x150-walk-right-10.png");	
        images.addImage("playerman100x150-walk-right-11.png");	
	imagesleft = new StateImageLibrary();
        imagesleft.addImage("playerman100x150-walk-left-1.png");	
        imagesleft.addImage("playerman100x150-walk-left-2.png");	
        imagesleft.addImage("playerman100x150-walk-left-3.png");	
        imagesleft.addImage("playerman100x150-walk-left-4.png");	
        imagesleft.addImage("playerman100x150-walk-left-5.png");	
        imagesleft.addImage("playerman100x150-walk-left-6.png");	
        imagesleft.addImage("playerman100x150-walk-left-7.png");	
        imagesleft.addImage("playerman100x150-walk-left-8.png");	
        imagesleft.addImage("playerman100x150-walk-left-9.png");	
        imagesleft.addImage("playerman100x150-walk-left-10.png");	
        imagesleft.addImage("playerman100x150-walk-left-11.png");	
}

public Image getImage()
{

	if (imagesindex > 11)//NOTE hardcoded number of images
		imagesindex = 0; 

	if (key == KeyEvent.VK_F && prevdirection == "left")
		return leftshootimage; 
	else if (key == KeyEvent.VK_F && prevdirection == "right")
		return rightshootimage; 
	
	if (direction == "right")
		return images.getImage(imagesindex);
	else if (direction == "left")
		return imagesleft.getImage(imagesindex);
	//else if (prevdirection == "nowhere")
	//	return rightimagestanding;
	else
		return images.getImage();
}

public void moveP()//FIXME moveP
{
  	int foo = movePWithKeyLevel3(key);
	if (foo > 0 && getx() >= 394) {
		//FIXME mapx+=5.0;
      	}

  	shootP(key, friendlybullets);
	fall();
}

public void fall()
{
	    if (jumpcounter > 0) {
	    	playery -= 3*jumpcounter++;
		//playerx +=15;
	   }
	   if (jumpcounter > 11)
		jumpcounter = 0;

	    playery += 3;
	    if (playery > 400)
		playery = 400-1;

}

public int movePWithKeyLevel3(int key)
{
	   int foo = 0;
           if (key == KeyEvent.VK_LEFT)//NOTE1
            {
		playerx += -5;
		direction = "left";
		prevdirection = "left";
		imagesindex++;
		foo = 1;
            }
            else if (key == KeyEvent.VK_RIGHT)
            {
		playerx += 5;
		direction = "right";
		prevdirection = "right";
		imagesindex++;
		foo = 2;
            }
	    else if (key == KeyEvent.VK_UP)
	    {
		jumpcounter++;
	    }
	    else 
	    {
		//direction = "nowhere";
		//prevdirection = "nowhere";
	    }

	   if (foo > 0 && playerx >= 400)
		playerx -= 5;
	return foo;
}

public void shootP(int key, LinkedList<Bullet> l)
{
            if (key == KeyEvent.VK_F)
            {
		if (prevdirection == "right") {
			Bullet bullet = new Bullet(getx()+50,gety()+80,"bullet16x16-1.png");
			bullet.setdx(+6);
			l.add(bullet);
		}
		else if (prevdirection == "left") {
			Bullet bullet = new Bullet(getx(),gety()+80,"bullet16x16-1.png");
			bullet.setdx(-6);
			l.add(bullet);
		}
		/*else if (direction == "nowhere") {
			Bullet bullet = new Bullet(getx(),gety()+80,"bullet16x16-1.png");
			bullet.setdx(-6);
			l.add(bullet);
		}*/
            }
}

public boolean collisionLevel3(Entity e, int mapx)
{
	if (distance(e.getx()-mapx, playerx, e.gety(), playery) < 30) {
		System.out.println("collision with player...gameover.");
		System.exit(99);	
		return true;
	}
	else
		return false;
}
};

